package com.shundr.truck.adapter;

import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.shundr.auth.channel.R;
import com.shundr.auth.common.util.LImageLoader;
import com.shundr.auth.frame.util.StringUtil;
import com.shundr.truck.model.TruckInfo;

/**
 * 我的车辆列表适配器 TruckAdapter
 * 
 * @author larry 2015-4-27 下午4:30:10
 * @version 1.0.0
 *
 */
public class SocialTruckAdapter extends ArrayAdapter<TruckInfo> {

	public SocialTruckAdapter(Context context, List<TruckInfo> list) {
		super(context, 0, list);
		this.mContext = context;
		this.mList = list;
		this.mImageLoader = ImageLoader.getInstance();
	}

	private List<TruckInfo> mList;
	private Context mContext;
	private ImageLoader mImageLoader;

	@SuppressLint("NewApi")
	public void setData(List<TruckInfo> list) {
		mList = list;
		// clear();
		// addAll(list);
		notifyDataSetInvalidated();
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public TruckInfo getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHold viewHold = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_social_truck_info, null);
			viewHold = new ViewHold();
			viewHold.TruckCount = (TextView) convertView
					.findViewById(R.id.tv_type);
			viewHold.TruckNum = (TextView) convertView
					.findViewById(R.id.tv_code);
			viewHold.TruckPicture = (ImageView) convertView
					.findViewById(R.id.iv_item_truck_picture);
			viewHold.tvStatus = (TextView) convertView
					.findViewById(R.id.tv_Status);
			convertView.setTag(viewHold);
		} else {
			viewHold = (ViewHold) convertView.getTag();
		}
		try {
			TruckInfo info = mList.get(position);
			viewHold.TruckNum.setText(info.getTruckPlateNumber() + "    "
					+ info.truckerPhone);
			viewHold.TruckCount.setText(info.getTruckType() + " "
					+ info.getTruckCapacity() + " 吨 " + info.getTruckLength()
					+ "米");

			switch (info.getUpStatus()) {
			case 0:
				viewHold.tvStatus.setText("未上传");
				viewHold.tvStatus
						.setBackgroundResource(R.drawable.tv_green_bg_circle_corner);
				break;
			case 1:
				viewHold.tvStatus.setText("已上传");
				viewHold.tvStatus
						.setBackgroundResource(R.drawable.tv_green_bg_circle_corner);
				break;
			case 2:
				viewHold.tvStatus.setText("上传失败");
				viewHold.tvStatus
						.setBackgroundResource(R.drawable.tv_orange_bg_circle_corner);
				break;

			default:
				break;
			}

			mImageLoader.displayImage("file://" + info.getTruckPic(),
					viewHold.TruckPicture, LImageLoader.mOptions);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	class ViewHold {
		/**
		 * 车的序号
		 */
		TextView TruckCount;
		/**
		 * 车牌号码
		 */
		TextView TruckNum;
		/**
		 * 车辆照片
		 */
		ImageView TruckPicture;

		TextView tvStatus;
	}
}
